#coding=utf-8
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
from django.db import models

# Create your models here.
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from userena.models import UserenaBaseProfile


class MyProfile(UserenaBaseProfile):
    user = models.OneToOneField(User,
                                unique=True,
                                verbose_name=_('user'),
                                related_name='my_profile')
    favourite_snack = models.CharField(_('favourite snack'),
                                       max_length=5)

#作者的数据模型
@python_2_unicode_compatible
class Author(models.Model):
    name = models.CharField('用户名（name)', max_length=256)
    password = models.CharField('密码（password）', max_length=256)
    profile = models.CharField('profile', default='',max_length=256)

    register_date = models.DateTimeField(auto_now_add=True, editable=True)

    def __str__(self):
        return self.name
    class Meta:
        verbose_name = 'Author'
        verbose_name_plural = '作者'

#column字段是文章所属的分类，文章和分类是多对一的关系。我们构建一个Column数据模型
@python_2_unicode_compatible
class Column(models.Model):
    name = models.CharField( '分类名称', max_length=256,)
    intro = models.TextField('介绍', default='')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'column'
        verbose_name_plural = '分类'
        ordering = ['name']



#Article模型的管理器，在models.py中加入以下代码(需写在class Article前面
class ArticleManager(models.Manager):
    def query_by_column(self, column_id):
        query = self.get_queryset().filter(column_id=column_id)

    def query_by_user(self, user_id):
        user = User.objects.get(id=user_id)
        article_list = user.article_set.all()
        return article_list

    def query_by_polls(self):
        query = self.get_queryset().order_by('-zanUp_num') #order_by查询结果排序  点赞 高到低
        return query

    def query_by_time(self):
        query = self.get_queryset().order_by('-update_time') # 在 column name 前加一个负号，可以实现倒序
        return query

    def query_by_keyword(self, keyword):
        query = self.get_queryset().filter(title__contains=keyword)
        return query

##创建文章(article)这个数据模型
@python_2_unicode_compatible
class Article(models.Model):
    column = models.ForeignKey(Column, blank=True, null=True, verbose_name='类型')
    title = models.CharField('标题',max_length=255, unique=True)
    author = models.ForeignKey('Author',verbose_name='作者')
    content = models.TextField('内容')
    tag1 = models.CharField('标签1',max_length=256)
    tag2 = models.CharField('标签2', null=True,max_length=256)
    tag3 = models.CharField('标签3', null=True,max_length=256)
    creation_date = models.DateTimeField('创建时间', auto_now_add=True, editable=True, null=True)
    update_time = models.DateTimeField('更新时间', auto_now=True, null=True)
    published = models.BooleanField('是否发布', default=True)
    zanUp_num = models.IntegerField('喜欢点赞', default=0)
    zanDown_num = models.IntegerField('不喜欢点下', default=0)
    comment_num = models.IntegerField('评论', default=0)
    keep_num = models.IntegerField('点击数', default=0)

    object = ArticleManager()
    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'article'
        verbose_name_plural = '文章(article)'

class ImgManager(models.Manager):
    def query_by_column(self, column_id):
        query = self.get_queryset().filter(column_id=column_id)

    def query_by_user(self, user_id):
        user = User.objects.get(id=user_id)
        article_list = user.article_set.all()
        return article_list

    def query_by_polls(self):
        query = self.get_queryset().order_by('-zanUp_num') #order_by查询结果排序  点赞 高到低
        return query

    def query_by_time(self):
        query = self.get_queryset().order_by('-update_time') # 在 column name 前加一个负号，可以实现倒序
        return query

    def query_by_keyword(self, keyword):
        query = self.get_queryset().filter(title__contains=keyword)
        return query

#动图
class Img(models.Model):
    column = models.ForeignKey('Column', blank=True, null=True, verbose_name='类型')
    title = models.CharField('标题',max_length=255,null=True, unique=True)
    author = models.ForeignKey('Author', blank=True, null=True, verbose_name='作者')
    img_url = models.CharField('url地址', max_length=256)
    img_name = models.CharField('img_name', max_length=256)
    tag1 = models.CharField('标签1',max_length=256)
    tag2 = models.CharField('标签2', null=True,max_length=256)
    tag3 = models.CharField('标签3', null=True,max_length=256)
    creation_date = models.DateTimeField('创建时间', auto_now_add=True, editable=True, null=True)
    update_time = models.DateTimeField('更新时间', auto_now=True, null=True)
    published = models.BooleanField('是否发布', default=True)
    zanUp_num = models.IntegerField('喜欢点赞', default=0)
    zanDown_num = models.IntegerField('不喜欢点下', default=0)
    comment_num = models.IntegerField('评论', default=0)
    keep_num = models.IntegerField('点击数', default=0)
    #tag = models.ManyToManyField('Tags')

    object = ImgManager()
    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Img图片'
        verbose_name_plural = '图片'



#评论的数据模型
@python_2_unicode_compatible
class Comment(models.Model):
    user = models.ForeignKey('Author', null=True)
    article = models.ForeignKey(Article, null=True)
    content = models.TextField()
    creation_date = models.DateTimeField('创建时间', auto_now_add=True, editable=True, null=True)
    zanUp_num = models.IntegerField('喜欢点赞', default=0)
    zanDown_num = models.IntegerField('不喜欢点下', default=0)

    def __str__(self):
        return self.content
    class Meta:
        verbose_name = 'commen'
        verbose_name_plural = '评论'


###  上转文件用
class uploadfire(models.Model):
    username = models.CharField(max_length = 30)
    headImg = models.FileField(upload_to = './upload/')

    def __unicode__(self):
        return self.username